package db;

import java.lang.reflect.ParameterizedType;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpSession;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class HibernateUtil<E> {
	private Class<E> clazz;
	 private EntityManager entityManager;
	 public void create(E entity) {
		    Transaction tx = null;
		    Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		    try {
		      tx = session.beginTransaction();
		      session.save(entity);
		      tx.commit();
		    } catch (RuntimeException e) {
		      if (tx != null && tx.isActive()) {
		        try {
		          tx.rollback();
		        } catch (HibernateException e1) {
		        }
		        throw e;
		      }
		    }
	 }
	 public E getById(int id) {

		    Transaction tx = null;
		    Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();

		    session.beginTransaction();

		    E entity = (E) session.get(this.getClass(), id);
	
		    session.getTransaction().commit();
	
		    return entity;


		    }
		  
}
